{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(19, 10)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['Customer ID', '医院类型', '医院等级', 'BG', 'BU', '可能性', '数量', '状态', '竞争对手',\n",
       "       '客户选择倾向'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.model_selection import train_test_split \n",
    "\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "\n",
    "from sklearn import metrics \n",
    "\n",
    "\n",
    "\n",
    "data_cart = pd.read_excel('G:\\AI\\决策树分类算法\\FunnelList_info.xlsx',sep = ',',encoding = 'utf-8')\n",
    "\n",
    "print(data_cart.shape)\n",
    "\n",
    "data_cart.columns\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Customer ID</th>\n",
       "      <th>医院类型</th>\n",
       "      <th>医院等级</th>\n",
       "      <th>BG</th>\n",
       "      <th>BU</th>\n",
       "      <th>可能性</th>\n",
       "      <th>数量</th>\n",
       "      <th>状态</th>\n",
       "      <th>竞争对手</th>\n",
       "      <th>客户选择倾向</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>33</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>90</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Siemens (西门子)</td>\n",
       "      <td>竞争对手</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>32</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>Mindray Medical (迈瑞)</td>\n",
       "      <td>未知</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>没有透露/未知</td>\n",
       "      <td>竞争对手</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Local Supplier (本地竞争对手)</td>\n",
       "      <td>竞争对手</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Other Global Competitor (其他国际品牌)</td>\n",
       "      <td>竞争对手</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Customer ID  医院类型  医院等级  BG  BU  可能性  数量  状态  \\\n",
       "0            5     0    33   1   1   90   1   1   \n",
       "1            6     0    32   2   2    0   2   1   \n",
       "2            7     0    30   2   2    0   1   1   \n",
       "3            8     0    20   2   2    0   1   1   \n",
       "4            9     0    20   2   2    0   1   1   \n",
       "\n",
       "                               竞争对手 客户选择倾向  \n",
       "0                     Siemens (西门子)   竞争对手  \n",
       "1              Mindray Medical (迈瑞)     未知  \n",
       "2                           没有透露/未知   竞争对手  \n",
       "3           Local Supplier (本地竞争对手)   竞争对手  \n",
       "4  Other Global Competitor (其他国际品牌)   竞争对手  "
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_cart.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='gini',\n",
       "                       max_depth=None, max_features=None, max_leaf_nodes=None,\n",
       "                       min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "                       min_samples_leaf=1, min_samples_split=2,\n",
       "                       min_weight_fraction_leaf=0.0, presort='deprecated',\n",
       "                       random_state=None, splitter='best')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "array = data_cart.values\n",
    "\n",
    "X =array[:,0:8]  \n",
    "\n",
    "Y = array[:,8]\n",
    "\n",
    "test_size = 0.30\n",
    "\n",
    "seed = 4\n",
    "\n",
    "X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)\n",
    "\n",
    "# 一般形式：X_train,X_test, y_train, y_test =cross_validation.train_test_split(train_data,train_target,test_size= , random_state= )\n",
    "\n",
    "#X_train：所要划分的样本特征集\n",
    "\n",
    "#train_target：所要划分的样本结果\n",
    "\n",
    "#test_size：样本占比，如果是整数的话就是样本的数量\n",
    "\n",
    "#random_state：是随机数的种子。在需要重复试验的时候，保证得到一组一样的随机数。\n",
    "\n",
    "# 采用sklearn模块构建cart决策树\n",
    "\n",
    "cart_tree = DecisionTreeClassifier() \n",
    "\n",
    "cart_tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3333333333333333"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cart_tree.fit(X_train, Y_train)\n",
    "\n",
    "data_cart_pre = cart_tree.predict(X_test)\n",
    "\n",
    "sum(data_cart_pre == Y_test)/float(len(Y_test)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cart_tree_1.pdf'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import graphviz\n",
    "\n",
    "import sklearn.tree as tree\n",
    "\n",
    "dot_data = tree.export_graphviz(cart_tree, out_file=None) \n",
    "\n",
    "graph = graphviz.Source(dot_data) \n",
    "\n",
    "graph.render(\"cart_tree_1\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
